<template>
  <div id="postmanage">
    <!--搜索表单开始-->
    <div class="search" style="margin-bottom: 20px">
      <el-input
        style="width: 240px"
        placeholder="请输入相关内容"
        v-model="content"
      ></el-input>
      <el-select
        v-model="type"
        placeholder="请输入帖子类型"
        style="width: 240px; margin-left: 5px"
      >
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-button
        style="margin-left: 5px"
        type="primary"
        @click="(pageNum = 1), load()"
        ><i class="el-icon-search"></i> 搜索</el-button
      >
      <el-button style="margin-left: 5px" type="warning" @click="reset"
        ><i class="el-icon-refresh"></i> 重置</el-button
      >
    </div>
    <!-- 搜索表单结束 -->
    <!-- 表格开始 -->
    <div class="posttable">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="帖子内容">
                <span>{{ props.row.content }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="帖子 ID" prop="id"> </el-table-column>
        <el-table-column label="帖子类型" prop="type"> </el-table-column>
        <el-table-column label="发帖人账号" prop="userAccount">
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <!-- <el-button
              size="mini"
              style="margin-right: 6px"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            > -->
            <el-popconfirm
              title="确定删除吗？"
              @confirm="handleDelete(scope.$index, scope.row)"
            >
              <el-button slot="reference" size="mini" type="danger"
                >删除</el-button
              >
            </el-popconfirm>
            <!-- <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            > -->
          </template>
        </el-table-column>
      </el-table>
      <div style="display: flex; justify-content: center">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>

    <!-- 表格结束 -->
  </div>
</template>


<script>
import request from "@/utils/request";
export default {
  name: "HelloWorld",
  data() {
    return {
      options: [
        {
          value: "FIND_HOUSE",
          label: "找房源",
        },
        {
          value: "FIND_ROOMMATE",
          label: "找室友",
        },
        {
          value: "FIND_TENANT",
          label: "找租客",
        },
      ],
      type: "",
      total: 0,
      pageNum: 1,
      pageSize: 5,
      content: "",
      tableData: [],
    };
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      request
        .get("/mypost/page", {
          params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            type: this.type,
            content: this.content,
          },
        })
        .then((res) => {
          this.tableData = res.data.records;
          Promise.all(
            this.tableData.map((row) => this.getUserAccount(row.userId))
          ).then((accounts) => {
            for (let i = 0; i < this.tableData.length; i++) {
              this.$set(this.tableData[i], "userAccount", accounts[i]);
            }
          });
          this.total = res.data.total;
        });
    },
    reset() {
      this.pageNum = 1;
      this.pageSize = 5;
      this.content = "";
      this.type = "";
      this.load();
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.load();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val;
      this.load();
    },
    handleDelete(index, row) {
      console.log(row.id);
      request.delete("/mypost/" + row.id).then((res) => {
        if (res.code === 200) {
          this.$notify.success("删除成功");
          this.load();
        } else {
          this.$notify.error(res.msg);
        }
      });
    },
    async getUserAccount(id) {
      if (id != null) {
        try {
          const res = await request.get("/user/" + id);
          if (res.code === 200) {
            console.log(res.data.userName);
            return res.data.userName;
          } else {
            this.$notify.error(res.msg);
            return "";
          }
        } catch (error) {
          console.error(error);
          this.$notify.error("获取用户账号失败");
          return "";
        }
      }
    },
  },
};
</script>


<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.search {
  width: 675px;
  margin-left: 80px;
}
.el-button--primary {
  color: #fff;
  background-color: rgb(0, 184, 224);
  border-color: rgb(0, 184, 224);
}
/* 表格样式开始 */
.posttable {
  width: 1050px;
  /* margin-left: 350px; */
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
/* 表格样式结束 */
</style>